const proxyConfigs = {
  "risk-map-api": {
    host: "http://10.13.1.224:9198/riskMapApi/",
    // host: "http://127.0.0.1:5346/riskMapApi/",
    // host: "http://10.20.88.97:9198/riskMapApi/",
    // host: "http://10.20.88.119:9198/riskMapApi/",
    path: "",
  },
}

function wrapKeyWithMark(key, mark) {
  let wrappedKey = key

  if (!wrappedKey.startsWith(mark)) {
    wrappedKey = mark.concat(wrappedKey)
  }

  if (!wrappedKey.endsWith(mark)) {
    wrappedKey = wrappedKey.concat(mark)
  }

  return wrappedKey
}

function wrapKeyWithSlash(key) {
  return wrapKeyWithMark(key, '/')
}

module.exports = () => {
  let proxy = {}
  Object.keys(proxyConfigs).forEach((configKey) => {
    if (Object.hasOwnProperty.call(proxyConfigs, configKey)) {
      const { host } = proxyConfigs[configKey]
      let proxyKey = wrapKeyWithSlash(configKey)

      proxy[proxyKey] = {
        target: host,
        pathRewrite: {
          [`^${proxyKey}`]: '',
        },
        changeOrigin: true,
        secure: false,
      }
    }
  })

  return proxy
}
